﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace eCompany.Forms
{
    public partial class add_company_form : Form
    {
        private eCompany.eObj.organisation m_org = null;

        public add_company_form(ref eCompany.eObj.organisation org)
        {
            InitializeComponent();
            this.Icon = pics.eCompanyIcon;
            m_org = org;
            InitForm();
        }

        private void InitForm()
        {
            companyTypeComboBoxExtended.ItemHeight = typeImageList.ImageSize.Height;

            foreach (eCompany.eObj.company_type_enum e in Enum.GetValues(typeof(eCompany.eObj.company_type_enum)))
            {
                Image img = Tools.GetCompanyTypeImage(e);
                typeImageList.Images.Add(e.ToString(), img);
                ComboBoxExtendedItem item = new ComboBoxExtendedItem();
                item.Obj = e;
                item.ImageTag = e.ToString();
                companyTypeComboBoxExtended.Items.Add(item);
            }

            foreach (string cur in eCompany.eManager.eManager.m_currency_code_list)
            {
                Image img = Tools.GetCurrencyImage(cur);
                currencyImageList.Images.Add(cur, img);

                ComboBoxExtendedItem item = new ComboBoxExtendedItem();
                item.Obj = cur;
                item.ImageTag = cur;
                defaultAccountComboBoxExtended.Items.Add(item);
            }

            baseGldAccountValueTxt.Text = "0";
            baseAccountValueTxt.Text = "0";
            baseCompanyStockValueTxt.Text = "0";
            baseMaterialTxt.Text = "0";
        }

        private void createCmd_Click(object sender, EventArgs e)
        {
            eManager.eManager eM = new eManager.eManager();

            double dummy;

            if(companyNameTxt.Text != "" &&
                companyTypeComboBoxExtended.Text != "" &&
                baseGldAccountValueTxt.Text != "" && double.TryParse(baseGldAccountValueTxt.Text,out dummy) &&
                baseAccountValueTxt.Text != "" && double.TryParse(baseAccountValueTxt.Text,out dummy) &&
                baseCompanyStockValueTxt.Text != "" && double.TryParse(baseCompanyStockValueTxt.Text, out dummy) &&
                defaultAccountComboBoxExtended.Text != "" &&
                baseMaterialTxt.Text != "" && double.TryParse(baseMaterialTxt.Text, out dummy))
            {
                System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("en-US");
                string baseGldAccountValue = baseGldAccountValueTxt.Text.Replace(",", ".");
                string baseAccountValue = baseAccountValueTxt.Text.Replace(",", ".");
                string baseCompanyStockValue = baseCompanyStockValueTxt.Text.Replace(",", ".");
                string baseMaterialValue = baseMaterialTxt.Text.Replace(",", ".");

                eObj.company c = eM.addCompany(m_org, companyNameTxt.Text,
                        (eObj.company_type_enum)((ComboBoxExtendedItem) companyTypeComboBoxExtended.SelectedItem).Obj,
                        defaultAccountComboBoxExtended.Text,
                        Convert.ToDouble(baseAccountValue,culture),
                        Convert.ToDouble(baseGldAccountValue,culture),
                        Convert.ToDouble(baseCompanyStockValue,culture),
                        Convert.ToDouble(baseMaterialValue,culture));

                eM.addShop(c, defaultAccountComboBoxExtended.Text, 0);

                ((main)ParentForm).InitMenu();
                this.Close();
            }
        }
    }
}
